$(function() {
	var thisTime;
	$('.nav-ul li').mouseleave(function(even) {
		thisTime = setTimeout(thisMouseOut, 1000);
	})

	$('.nav-ul li').mouseenter(function() {
		clearTimeout(thisTime);
		var thisUB = $('.nav-ul li').index($(this));
		if ($.trim($('.nav-slide-o').eq(thisUB).html()) != "") {
			$('.nav-slide').addClass('hover');
			$('.nav-slide-o').hide();
			$('.nav-slide-o').eq(thisUB).show();
		} else {
			$('.nav-slide').removeClass('hover');
		}

	})

	function thisMouseOut() {
		$('.nav-slide').removeClass('hover');
	}

	$('.nav-slide').mouseenter(function() {
		clearTimeout(thisTime);
		$('.nav-slide').addClass('hover');
	})
	$('.nav-slide').mouseleave(function() {
		$('.nav-slide').removeClass('hover');
	})


	//时区转换,数据库取出的时间日期为utc
	function convertUTCTimeToLocalTime(UTCDateString) {
		if (!UTCDateString) {
			return '-';
		}

		function formatFunc(str) { //格式化显示
			return str > 9 ? str : '0' + str
		}
		var date2 = new Date(UTCDateString); //这步是关键
		var year = date2.getFullYear();
		var mon = formatFunc(date2.getMonth() + 1);
		var day = formatFunc(date2.getDate());
		var hour = date2.getHours();
		var noon = hour >= 12 ? 'PM' : 'AM';
		hour = hour >= 12 ? hour - 12 : hour;
		hour = formatFunc(hour);
		var min = formatFunc(date2.getMinutes());
		var dateStr = year + '-' + mon + '-' + day + ' ' + noon + ' ' + hour + ':' + min;
		return dateStr;
	}


	//出卷
	$('#chujuan').click(function(event) {
		event.preventDefault();
		$('#exam_search1').hide();
		$('#exam_info1').hide();
		$('#yfbtest1').hide();
		$('#updatetesttime1').hide();
		$('#class_search1').hide();
		$('#add_class1').hide();
		$('#searchstudentinfo1').hide();
		$('#studenttestinfo1').hide();
		$('.wrap').hide();
		$('#chujuan1').show();

		//因为要出10道题
		for (var i = 1; i <= 10; i++) {
			var sj = `<div class="form-group" style="margin-top: 10px;" id=div${i}>第${i}题
			题目：<input type="text" class="form-control" placeholder="请输入题目" />
			选项： <input type="text" class="form-control" placeholder="格式为:xxx;xxx;xxx;xxx" />
			答案：<input type="text" class="form-control" placeholder="请输入答案，例如A" />
			</div>`;
			$("#chujuan1").append(sj);
		}
		var tijiao = `<button type="button" id="tijiao" class="btn btn-success">提交</button>`;
		$("#chujuan1").append(tijiao);

		//提交试卷
		$('#tijiao').click(function(event) {
			event.preventDefault();
			var arr = [];
			for (var j = 1; j <= 10; j++) {
				var div = document.querySelectorAll(`#div${j}>input`); //获取每一组div的input值
				var obj = {
					"questionId": `${j}`,
					"questionTitle": `${div[0].value}`,
					"questionItems": `${div[1].value}`,
					"questionAnswer": `${div[2].value}`
				}
				arr.push(obj)
			}
			// console.log(arr)

			var arr2 = []; //创建数组,把所有input的值存入
			var input = document.querySelectorAll('#chujuan1 input'); //获取出卷界面下的所有input
			//console.log(input)
			for (var a = 0; a < input.length; a++) {
				arr2.push(input[a].value)

			}
			// console.log(arr2)
			//判断值是否为空
			function checkArray(my_arr) {
				for (var i = 0; i < my_arr.length; i++) {
					if (my_arr[i] === "")
						return false;
				}
				return true;
			}
			//如果为空则提示
			if (checkArray(arr2) == false) {
				alert('请输入完整');
			} else {
				var code = createSixNum();

				function createSixNum() {
					var Num = "";
					for (var i = 0; i < 6; i++) {
						Num += Math.floor(Math.random() * 10);
					}
					return Num;
				} //生成随机试卷编号
				//console.log(code)
				//传递对象数组需要JSON.stringify,序列化
				$.ajax({
					url: "v1/teachers/sj",
					type: "post",
					async: false,
					dataType: "json",
					traditional: true,
					data: {
						sj: JSON.stringify(arr),
						eid: code,
					},
					success: function(data) { //接收后端发送的数据
						if (data.status == 200) {
							alert('提交成功');
							$('#tijiao').prop('disabled', true);
							location.reload(); //刷新页面
						}
					},
					error: function(err) {
						console.log(err);
					},
				})
			}



		})
		$('#chujuan').unbind(); //解绑事件,使其在一次访问中只能够点击一次
	})



	//试卷查询
	$('#exam_search').click(function(event) {
		event.preventDefault();
		$('#chujuan1').hide();
		$('#exam_info1').hide();
		$('#yfbtest1').hide();
		$('#updatetesttime1').hide();
		$('#class_search1').hide();
		$('#add_class1').hide();
		$('#searchstudentinfo1').hide();
		$('#studenttestinfo1').hide();
		$('.wrap').hide();
		$('#exam_search1').show();
		var exam_search = (function() {
			var result;
			$.ajax({
				type: 'get',
				url: `/v1/teachers/examsearch`,
				async: false,
				success: function(data) {
					result = data.data;
				}
			});
			return result;
		})();
		// console.log(exam_search)
		for (let a = 0; a < exam_search.length; a++) {
			document.querySelector('#exam_search2>tbody').innerHTML += `
							  <tr>
							    <td>${exam_search[a].e_id}</td>
							    <td>${exam_search[a].eid}</td>
							    <td>${exam_search[a].e_sj}</td>
							  </tr>
							`
		}
		$('#exam_search').unbind(); //解绑事件,使其在一次访问中只能够点击一次
	})



	//发布考试
	$('#exam_info').click(function(event) {
		event.preventDefault();
		$('#chujuan1').hide();
		$('#exam_search1').hide();
		$('#yfbtest1').hide();
		$('#updatetesttime1').hide();
		$('#class_search1').hide();
		$('#add_class1').hide();
		$('#searchstudentinfo1').hide();
		$('#studenttestinfo1').hide();
		$('.wrap').hide();
		$('#exam_info1').show();

		//查询试卷id
		var select = (function() {
			var result;
			$.ajax({
				type: 'get',
				url: `/v1/teachers/examsearch`,
				async: false,
				success: function(data) {
					result = data.data;
				}
			});
			return result;
		})();
		// console.log(select)

		//查询发布试卷的id
		var examinfo = (function() {
			var result;
			$.ajax({
				type: 'get',
				url: `/v1/teachers/examinfo`,
				async: false,
				success: function(data) {
					result = data.data;
				}
			});
			return result;
		})();
		// console.log(examinfo)

		//查询班级
		var selectclassroom = (function() {
			var result;
			$.ajax({
				type: 'get',
				url: `/v1/teachers/selectclassroom`,
				async: false,
				success: function(data) {
					result = data.data;
				}
			});
			return result;
		})();

		//把班级渲染到下拉框
		for (let x = 0; x < selectclassroom.length; x++) {
			document.getElementById('classroom').innerHTML +=
				`<option  value =${selectclassroom[x].classroom}>${selectclassroom[x].classroom}</option>`
		}

		//把试卷id渲染到下拉框
		for (var x = 0; x < select.length; x++) {
			document.getElementById('selecteid').innerHTML +=
				`<option id=${select[x].eid} value =${select[x].eid}>${select[x].eid}</option>`
		}

		//如果存在已发布过试卷id，则当前select禁用
		if (examinfo != undefined) {
			for (var xx = 0; xx < examinfo.length; xx++) {
				$(`#${select[xx].eid}`).prop('disabled', true);
				document.getElementById(`${select[xx].eid}`).innerHTML += "已发布"
			}
		}




		//发布
		$('#fabu').click(function(event) {
			event.preventDefault()
			//表单验证
			let testname = $('#testname').val();
			let classroom = $('#classroom').val();
			let selecteid = $('#selecteid').val();
			console.log(selecteid)
			let starttime = $('#starttime').val();
			let endtime = $('#endtime').val();
			if (testname == "" || classroom == "" || selecteid == "" || starttime == "" ||
				endtime == "") {
				alert('请填写完整');
			} else {
				$.ajax({
					url: "v1/teachers/examinfo",
					type: "post",
					async: false,
					data: {
						testname: testname,
						classroom: classroom,
						selecteid: selecteid,
						starttime: starttime,
						endtime: endtime,
					},
					success: function(data) { //接收后端发送的数据
						if (data.status == 200) {
							alert('发布成功');
							$('#fabu').attr('disabled', true)
							location.reload(); //刷新页面
						}
					},
					error: function(err) {
						console.log(err);
						alert('考试名称不能重复')
					},
				})
			}

		})

		$('#exam_info').unbind(); //解绑事件,使其在一次访问中只能够点击一次
	})


	//查询已发布的考试
	$('#yfbtest').click(function(event) {
		event.preventDefault()
		$('#exam_search1').hide();
		$('#exam_info1').hide();
		$('#chujuan1').hide();
		$('#updatetesttime1').hide();
		$('#class_search1').hide();
		$('#add_class1').hide();
		$('#searchstudentinfo1').hide();
		$('#studenttestinfo1').hide();
		$('.wrap').hide();
		$('#yfbtest1').show();

		//查询已发布的考试信息
		var yfb = (function() {
			var result;
			$.ajax({
				type: 'get',
				url: `/v1/teachers/examinfo`,
				async: false,
				success: function(data) {
					result = data.data;
				}
			});
			return result;
		})();
		// console.log(yfb)

		for (var zz = 0; zz < yfb.length; zz++) {
			var starttime = convertUTCTimeToLocalTime(yfb[zz].start_time)
			var endtime = convertUTCTimeToLocalTime(yfb[zz].end_time)
			document.querySelector('#yfbtest2>tbody').innerHTML += `
							        <tr>
							          <td>${yfb[zz].e_name}</td>
							          <td>${yfb[zz].eid}</td>
							          <td>${starttime}</td>
							          <td>${endtime}</td>
							          <td><button type="button" class="btn btn-success">删除</button></td>
							        </tr>
							`
		}




		let buttons = document.querySelectorAll('#yfbtest2 button')
		for (let btn of buttons) {
			btn.onclick = function() {
				//获得当前按钮所在的行tr——要删除的行,父级的父级，button的父级是td，td的父级才是tr
				let tr = this.parentElement.parentElement;
				let tbody = document.querySelector('#yfbtest2 tbody')
				// console.log(this.parentElement)

				//获得当前行的班级名称
				let testname = tr.children[0].innerHTML;
				let r = confirm(`是否要删除${testname}`)
				if (r == true) {
					console.log(tbody)
					//删除该行
					tbody.removeChild(tr);
					$.ajax({
						url: `v1/teachers/deletetest/${testname}`,
						type: "delete",
						async: false,
						success: function(data) { //接收后端发送的数据
							if (data.status == 200) {
								alert('删除成功');
								// location.reload(); //刷新页面
							}
						},
						error: function(err) {
							console.log(err);
							alert(err)
						},
					})
				} else {
					alert('你已取消')
				}
			}
		}



		$('#yfbtest').unbind(); //解绑事件,使其在一次访问中只能够点击一次
	})



	//更改考试时间
	$('#updatetesttime').click(function(event) {
		event.preventDefault()
		$('#exam_search1').hide();
		$('#exam_info1').hide();
		$('#chujuan1').hide();
		$('#yfbtest1').hide();
		$('#class_search1').hide();
		$('#add_class1').hide();
		$('#searchstudentinfo1').hide();
		$('#studenttestinfo1').hide();
		$('.wrap').hide();
		$('#updatetesttime1').show();

		//获取已发布的考试信息(主要是考试名称)
		var ename = (function() {
			var result;
			$.ajax({
				type: 'get',
				url: `/v1/teachers/examinfo`,
				async: false,
				success: function(data) {
					result = data.data;
				}
			});
			return result;
		})();

		for (let a = 0; a < ename.length; a++) {
			document.querySelector('[data-id="testname"]').innerHTML +=
				`<option  value =${ename[a].e_name}>${ename[a].e_name}</option>`
		}

		$('[data-id="update"]').click(function(event) {
			event.preventDefault()
			let ename = $('[data-id="testname"]').val();
			let starttime = $('[data-id="starttime"]').val();
			let endtime = $('[data-id="endtime"]').val();

			if (ename == "" || starttime == "" || endtime == "") {
				alert('请填写完整')
			} else {
				$.ajax({
					url: "v1/teachers/examtime",
					type: "put",
					async: false,
					data: {
						ename: ename,
						starttime: starttime,
						endtime: endtime,
					},
					success: function(data) { //接收后端发送的数据
						if (data.status == 200) {
							alert('修改成功');
							$('[data-id="update"]').attr('disabled', true)
							location.reload(); //刷新页面
						}
					},
					error: function(err) {
						console.log(err);
						alert(err)
					},
				})
			}
		})

		$('#updatetesttime').unbind(); //解绑事件,使其在一次访问中只能够点击一次
	})



	//班级查询
	$('#class_search').click(function(event) {
		event.preventDefault()
		$('#exam_search1').hide();
		$('#exam_info1').hide();
		$('#chujuan1').hide();
		$('#yfbtest1').hide();
		$('#updatetesttime1').hide();
		$('#add_class1').hide();
		$('#searchstudentinfo1').hide();
		$('#studenttestinfo1').hide();
		$('.wrap').hide();
		$('#class_search1').show();

		//查询班级
		var class_room = (function() {
			var result;
			$.ajax({
				type: 'get',
				url: `/v1/teachers/selectclassroom`,
				async: false,
				success: function(data) {
					result = data.data;
				}
			});
			return result;
		})();

		//把班级渲染到表格
		for (let x = 0; x < class_room.length; x++) {
			document.querySelector('#class_search2>tbody').innerHTML +=
				`
					<tr>
						<td>${class_room[x].classroom}</td>
						<td><button type="button" class="btn btn-success">删除</button></td>
					</tr>
				`
		}
		let buttons = document.querySelectorAll('#class_search2 button')
		for (let btn of buttons) {
			btn.onclick = function() {
				//获得当前按钮所在的行tr——要删除的行,父级的父级，button的父级是td，td的父级才是tr
				let tr = this.parentElement.parentElement;
				// console.log(this.parentElement)

				//获得当前行的班级名称
				let classroom = tr.children[0].innerHTML;
				let r = confirm(`是否要删除${classroom}`)
				if (r == true) {
					let tbody = document.querySelector('#class_search2>tbody')
					//删除该行
					tbody.removeChild(tr);
					$.ajax({
						url: `v1/teachers/deleteclassroom/${classroom}`,
						type: "delete",
						async: false,
						success: function(data) { //接收后端发送的数据
							if (data.status == 200) {
								alert('删除成功');
								// location.reload(); //刷新页面
							}
						},
						error: function(err) {
							console.log(err);
							alert(err)
						},
					})
				} else {
					alert('你已取消')
				}
			}
		}

		$('#class_search').unbind(); //解绑事件,使其在一次访问中只能够点击一次
	})

	//添加班级
	$('#add_class').click(function(event) {
		event.preventDefault()
		$('#exam_search1').hide();
		$('#exam_info1').hide();
		$('#chujuan1').hide();
		$('#yfbtest1').hide();
		$('#updatetesttime1').hide();
		$('#class_search1').hide();
		$('#searchstudentinfo1').hide();
		$('#studenttestinfo1').hide();
		$('.wrap').hide();
		$('#add_class1').show();


		$('#add_class1>#addclass').click(function(event) {
			event.preventDefault()
			let classroom = $('[data-classroom]').val();
			if (classroom == "") {
				alert('请输入班级')
			} else {
				$.ajax({
					url: `v1/teachers/addclassroom`,
					type: "post",
					async: false,
					data: {
						classroom: classroom
					},
					success: function(data) { //接收后端发送的数据
						if (data.status == 200) {
							alert('添加成功');
							location.reload(); //刷新页面
						}
					},
					error: function(err) {
						console.log(err);
						alert(err)
					},
				})
			}


		})


		$('#add_class').unbind(); //解绑事件,使其在一次访问中只能够点击一次
	})

	//学生信息查询
	$('#searchstudentinfo').click(function(event) {
		event.preventDefault()
		$('#exam_search1').hide();
		$('#exam_info1').hide();
		$('#chujuan1').hide();
		$('#yfbtest1').hide();
		$('#updatetesttime1').hide();
		$('#class_search1').hide();
		$('#add_class1').hide();
		$('#studenttestinfo1').hide();
		$('.wrap').hide();
		$('#searchstudentinfo1').show();

		//获取学生信息
		let studentinfomation = (function() {
			var result;
			$.ajax({
				type: 'get',
				url: `/v1/teachers/searchstudentinfo`,
				async: false,
				success: function(data) {
					result = data.data;
				}
			});
			return result;
		})();
		// console.log(studentinfomation)


		for (let a = 0; a < studentinfomation.length; a++) {
			//时间转换
			studentinfomation[a].birthday = convertUTCTimeToLocalTime(studentinfomation[a].birthday)

			//把性别变为文字
			if (studentinfomation[a].sex == 1) {
				studentinfomation[a].sex = "男";
			} else {
				studentinfomation.sex[a] = "女";
			}

		}



		//1. 先创建一个tbody
		var tbody = document.createElement("tbody");
		//2. 再遍历studentinfomation数组中每个学生对象
		for (var student of studentinfomation) {
			//每遍历一个学生对象，就创建一个tr，追加到tbody中
			var tr = document.createElement("tr");
			tbody.appendChild(tr);
			//3. 遍历当前学生对象中每个属性值
			for (var key in student) {
				// console.log(student)
				//每遍历一个属性值，就创建一个td，追加到tr中。并设置当前td的内容为当前属性的属性值
				var td = document.createElement("td");
				tr.appendChild(td);
				td.innerHTML = student[key];
			}

			//说明当前行的属性值td已经添加完成
			//就可以为当前行中再多添加一个td
			var td = document.createElement("td")
			tr.appendChild(td);
			//创建button，放入td中
			var button = document.createElement("button");
			button.innerHTML = "删除";
			button.className = "btn btn-success"
			td.appendChild(button);
			button.onclick = function() {
				//查找tbody
				tbody = document.querySelector(
					"#searchstudentinfo2>tbody"
				);
				//获得当前按钮所在的行tr——要删除的行
				tr = this.parentElement.parentElement;
				//获得当前行的员工姓名
				var uname = tr.children[0].innerHTML;
				var result = confirm(`是否继续删除${uname}?`)
				if (result == true) {
					//删除当前按钮所在行
					tbody.removeChild(tr);
					//数据库删除
					$.ajax({
						url: `v1/teachers/deletestudentinfo/${uname}`,
						type: "delete",
						async: false,
						success: function(data) { //接收后端发送的数据
							if (data.status == 200) {
								alert('删除成功');
							}
						},
						error: function(err) {
							alert(err)
						},
					})
				} else {
					alert('你已取消')
				}
			}
		}
		//3. 整个遍历结束后，再将tbody一次性追加到table中
		var table = document.querySelector("#searchstudentinfo2");
		table.appendChild(tbody);
		
		
			//一边输入一边搜索
		      $("[data-searchinput]").on("input", function () {
				// $("button").trigger("click");//常用事件可简写 触发器
				$("[data-searchbtn]").click();
		      });
			  
			  
		//按姓名查找学生信息
		$('[data-searchbtn]').click(function() {
			
			let name = $('[data-searchinput]').val()
			if(name==""){
				table.appendChild(tbody);
			}else{
				//先把表格内容置为空，因为后面采用了模糊查询，可能有多条数据，避免和一开始加载的内容重复
				document.querySelector('#searchstudentinfo2>tbody').innerHTML =""
				
				let bynamestudentinfomation = (function() {
					var result;
					$.ajax({
						type: 'get',
						url: `/v1/teachers/bynamesearchstudentinfo/${name}`,
						async: false,
						success: function(data) {
							result = data.data;
						}
					});
					return result;
				})();
				// console.log(bynamestudentinfomation)
				if (bynamestudentinfomation != undefined) {
					for (let a = 0; a < bynamestudentinfomation.length; a++) {
						//时间转换
						bynamestudentinfomation[a].birthday = convertUTCTimeToLocalTime(
							bynamestudentinfomation[a].birthday)
				
						//把性别变为文字
						if (bynamestudentinfomation[a].sex == 1) {
							bynamestudentinfomation[a].sex = "男";
						} else {
							bynamestudentinfomation.sex[a] = "女";
						}
						
						//更换tbody内容
						//因为采用了模糊查询,可能有多条数据，所以这里为+=
						document.querySelector('#searchstudentinfo2>tbody').innerHTML += `
							<tr>
								<td>${bynamestudentinfomation[a].u_name}</td>
								<td>${bynamestudentinfomation[a].phone}</td>
								<td>${bynamestudentinfomation[a].sex}</td>
								<td>${bynamestudentinfomation[a].birthday}</td>
								<td>${bynamestudentinfomation[a].email}</td>
								<td>${bynamestudentinfomation[a].classroom}</td>
								<td>${bynamestudentinfomation[a].name}</td>
								<td>${bynamestudentinfomation[a].studentid}</td>
								<td><button type="button" class="btn btn-success">删除</button></td>
							</tr>
							`
					}
				
				
					let buttons = document.querySelectorAll('#searchstudentinfo2 button')
					for (let btn of buttons) {
						btn.onclick = function() {
							//获得当前按钮所在的行tr——要删除的行,父级的父级，button的父级是td，td的父级才是tr
							let tr = this.parentElement.parentElement;
							// console.log(this.parentElement)
				
							//获得当前行的班级名称
							let name = tr.children[0].innerHTML;
							let r = confirm(`是否要删除${name}`)
							if (r == true) {
								let tbody = document.querySelector('#searchstudentinfo2>tbody')
								//删除该行
								tbody.removeChild(tr);
								$.ajax({
									url: `v1/teachers/deletestudentinfo/${name}`,
									type: "delete",
									async: false,
									success: function(data) { //接收后端发送的数据
										if (data.status == 200) {
											alert('删除成功');
											// location.reload(); //刷新页面
										}
									},
									error: function(err) {
										console.log(err);
										alert(err)
									},
								})
							} else {
								alert('你已取消')
							}
						}
					}
				
				
				}
				
			}
			
			
			
			

		})
		

		$('#searchstudentinfo').unbind(); //解绑事件,使其在一次访问中只能够点击一次
	})
	
	
	//学生考试信息
	$('#studenttestinfo').click(function(event){
		event.preventDefault()
		$('#exam_search1').hide();
		$('#exam_info1').hide();
		$('#chujuan1').hide();
		$('#yfbtest1').hide();
		$('#updatetesttime1').hide();
		$('#class_search1').hide();
		$('#add_class1').hide();
		$('#searchstudentinfo1').hide();
		$('.wrap').hide();
		$('#studenttestinfo1').show();
		
		
		
		
		//封装成函数，因为后面需要调用
		var studentexaminfofn=function(){
			
			//获取学生考试信息
			var studentexaminfo = (function() {
				var result;
				$.ajax({
					type: 'get',
					url: `/v1/teachers/searchstudentexaminfo`,
					async: false,
					success: function(data) {
						result = data.data;
					}
				});
				return result;
			})();
			// console.log(studentexaminfo)
			
			if(studentexaminfo!=undefined){
				//把信息渲染到表格
				for (let x = 0; x < studentexaminfo.length; x++) {
					studentexaminfo[x].time=convertUTCTimeToLocalTime(studentexaminfo[x].time)
					
					document.querySelector('#studenttestinfo2>tbody').innerHTML +=
						`
							<tr>
								<td>${studentexaminfo[x].e_name}</td>
								<td>${studentexaminfo[x].score}</td>
								<td>${studentexaminfo[x].time}</td>
								<td>${studentexaminfo[x].spendtime}</td>
								<td>${studentexaminfo[x].name}</td>
								<td>${studentexaminfo[x].u_name}</td>
								<td>${studentexaminfo[x].classroom}</td>
								<td>${studentexaminfo[x].studentid}</td>
								<td><button type="button" class="btn btn-success">删除</button></td>
							</tr>
						`
				}
				
				
				let buttons = document.querySelectorAll('#studenttestinfo2 button')
				for (let btn of buttons) {
					btn.onclick = function() {
						//获得当前按钮所在的行tr——要删除的行,父级的父级，button的父级是td，td的父级才是tr
						let tr = this.parentElement.parentElement;
						// console.log(this.parentElement)
				
						//获得当前行的考试名称
						let e_name = tr.children[0].innerHTML;
						//获得当前行的姓名
						let name = tr.children[4].innerHTML;
						let r = confirm(`是否要删除${name}的${e_name}`)
						if (r == true) {
							let tbody = document.querySelector('#studenttestinfo2>tbody')
							//删除该行
							tbody.removeChild(tr);
							$.ajax({
								url: `v1/teachers/deletestudentexaminfo/${name}&${e_name}`,
								type: "delete",
								async: false,
								success: function(data) { //接收后端发送的数据
									if (data.status == 200) {
										alert('删除成功');
										// location.reload(); //刷新页面
									}
								},
								error: function(err) {
									console.log(err);
									alert(err)
								},
							})
						} else {
							alert('你已取消')
						}
					}
				}
				
			}else{
				alert('暂无学生考试信息')
			}
			
		}
		
		studentexaminfofn()
		
		//一边输入一边搜索
		      $("[data-studentexaminfosearchinput]").on("input", function () {
				  // $("button").trigger("click");//常用事件可简写 触发器
		        $("[data-studentexaminfosearchbtn]").click();
		      });
		
		
		//按姓名查找学生考试信息
		$('[data-studentexaminfosearchbtn]').click(function(event){
			event.preventDefault();
			
			//先把表格内容置为空，因为后面采用了模糊查询，可能有多条数据，避免和一开始加载的内容重复
			document.querySelector('#studenttestinfo2>tbody').innerHTML=""
			
			let name=$('[data-studentexaminfosearchinput]').val();
			
			if(name==""){
				studentexaminfofn()
			}else{
				
				let bynamestudentexaminfo = (function() {
					var result;
					$.ajax({
						type: 'get',
						url: `/v1/teachers/bynamesearchstudentexaminfo/${name}`,
						async: false,
						success: function(data) {
							result = data.data;
						}
					});
					return result;
				})();
				// console.log(bynamestudentexaminfo)
				
				if(bynamestudentexaminfo!=undefined){
					
					for (let x = 0; x < bynamestudentexaminfo.length; x++) {
						bynamestudentexaminfo[x].time=convertUTCTimeToLocalTime(bynamestudentexaminfo[x].time)
						//因为采用了模糊查询,可能有多条数据，所以这里为+=
						document.querySelector('#studenttestinfo2>tbody').innerHTML +=
							`
								<tr>
									<td>${bynamestudentexaminfo[x].e_name}</td>
									<td>${bynamestudentexaminfo[x].score}</td>
									<td>${bynamestudentexaminfo[x].time}</td>
									<td>${bynamestudentexaminfo[x].spendtime}</td>
									<td>${bynamestudentexaminfo[x].name}</td>
									<td>${bynamestudentexaminfo[x].u_name}</td>
									<td>${bynamestudentexaminfo[x].classroom}</td>
									<td>${bynamestudentexaminfo[x].studentid}</td>
									<td><button type="button" class="btn btn-success">删除</button></td>
								</tr>
							`
					}
					
					
					let buttons = document.querySelectorAll('#studenttestinfo2 button')
					for (let btn of buttons) {
						btn.onclick = function() {
							//获得当前按钮所在的行tr——要删除的行,父级的父级，button的父级是td，td的父级才是tr
							let tr = this.parentElement.parentElement;
							// console.log(this.parentElement)
					
							//获得当前行的考试名称
							let e_name = tr.children[0].innerHTML;
							//获得当前行的姓名
							let name = tr.children[4].innerHTML;
							let r = confirm(`是否要删除${name}的${e_name}`)
							if (r == true) {
								let tbody = document.querySelector('#studenttestinfo2>tbody')
								//删除该行
								tbody.removeChild(tr);
								$.ajax({
									url: `v1/teachers/deletestudentexaminfo/${name}&${e_name}`,
									type: "delete",
									async: false,
									success: function(data) { //接收后端发送的数据
										if (data.status == 200) {
											alert('删除成功');
											// location.reload(); //刷新页面
										}
									},
									error: function(err) {
										console.log(err);
										alert(err)
									},
								})
							} else {
								alert('你已取消')
							}
						}
					}
					
					
				}
				
			}
			
			
			
			
			
		})
		
		
		
	$('#studenttestinfo').unbind(); //解绑事件,使其在一次访问中只能够点击一次	
		
	})
	
	
	
	
	//联系我们
	$('.contact').click(function(event){
		event.preventDefault()
		$('#exam_search1').hide();
		$('#exam_info1').hide();
		$('#chujuan1').hide();
		$('#yfbtest1').hide();
		$('#updatetesttime1').hide();
		$('#class_search1').hide();
		$('#add_class1').hide();
		$('#searchstudentinfo1').hide();
		$('#studenttestinfo1').hide();
		$('.wrap').show();
	})



})

window.onload = function() {
	$.ajax({
		url: "v1/users/searchicon",
		type: "post",
		async: false,
		success: function(data) {
			//console.log(data)
			if (data.status == 200) {
				//console.log(data.data)
				$('#imga').attr('src', data.data[0].url)

			} else {
				alert('失败');
			}

		},
		error: function(err) {
			console.log(err);
		},
	})
}
